package com.xnyzc.lhy.finance.controller.cashwithdrawal;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.finance.params.dto.cashwithdrawal.OaCashAmountDTO;
import com.xnyzc.lhy.finance.params.dto.cashwithdrawal.OaCashIdDTO;
import com.xnyzc.lhy.finance.params.dto.cashwithdrawal.OaCashWithdrawalRecordDTO;
import com.xnyzc.lhy.finance.service.cashwithdrawal.IOaDCashWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 * 司机提现表 前端控制器
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-10-16
 */
@RestController
@RequestMapping("/finance/oa-dcash-withdrawal")
public class OaDCashWithdrawalController {

    @Autowired
    private IOaDCashWithdrawalService iOaDCashWithdrawalService;

    /**
     * 获取提现记录
     * @param oaCashWithdrawalRecordDTO
     * @return
     */
    @PostMapping("getCashWithdrawalRecord")
    public Rv getCashDrawalRecord(@RequestBody OaCashWithdrawalRecordDTO oaCashWithdrawalRecordDTO) {
        Long driverId = TokenUtil.getTokenUserId();
        oaCashWithdrawalRecordDTO.setDriverId(driverId);
        return iOaDCashWithdrawalService.getCashDrawalRecord(oaCashWithdrawalRecordDTO);
    }

    /**
     * 获取提现详情
     * @param oaCashIdDTO 提现ID
     * @return
     */
    @PostMapping("getCashWithdrawalInfo")
    public Rv getCashWithdrawalInfo(
            @RequestBody OaCashIdDTO oaCashIdDTO
    ) {

        if (CheckUtil.objIsEmpty(oaCashIdDTO)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        return iOaDCashWithdrawalService.getCashWithdrawalInfo(Long.parseLong(oaCashIdDTO.getCashId()));
    }

    /**
     * 获取持卡人、银行卡、可提现金额等信息
     * @return Object
     */
    @PostMapping("getCashWithdrawalDetial")
    public Rv getCashWithdrawalDetial() {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDCashWithdrawalService.getCashWithdrawalDetial(driverId);
    }

    /**
     * 提现
     * @param oaCashAmountDTO 提现金额
     * @return Map
     */
    @PostMapping("driverCashWithdrawal")
    public Rv driverCashWithdrawal(
            @RequestBody OaCashAmountDTO oaCashAmountDTO
            ) {
        Long driverId = TokenUtil.getTokenUserId();

        if (CheckUtil.objIsEmpty(oaCashAmountDTO)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        // 效验金额
        String amount = oaCashAmountDTO.getAmount();

        amount = amount.replaceAll(",", "");
        amount = amount.replaceAll(" ", "");

        return iOaDCashWithdrawalService.driverCashWithdrawal(driverId, new BigDecimal(amount));
    }

    @GetMapping("clearDriverCashWithdrawal")
    public void clearDriverCashWithdrawal() {
        iOaDCashWithdrawalService.clearDriverCashWithdrawal();
    }

}
